{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d285bfcf-9ebb-43b7-ae01-885b08c676ee",
   "metadata": {},
   "source": [
    "## 鸢尾花数据集\n",
    "> 简介:\n",
    ">    ![tf-Iris-01.png](./img/tf-Iris-01.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7e4c2bfa-9d70-4592-91c9-1abf473b85bf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:21:49.110994Z",
     "iopub.status.busy": "2024-02-14T09:21:49.110005Z",
     "iopub.status.idle": "2024-02-14T09:21:49.136445Z",
     "shell.execute_reply": "2024-02-14T09:21:49.132664Z",
     "shell.execute_reply.started": "2024-02-14T09:21:49.110994Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2cc9bfb4-ece4-484f-8eb5-964dc25b7d1a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:19:59.375370Z",
     "iopub.status.busy": "2024-02-14T09:19:59.374449Z",
     "iopub.status.idle": "2024-02-14T09:19:59.396898Z",
     "shell.execute_reply": "2024-02-14T09:19:59.392349Z",
     "shell.execute_reply.started": "2024-02-14T09:19:59.374449Z"
    }
   },
   "outputs": [],
   "source": [
    "x_data = load_iris().data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e73cab00-fac6-471e-bddd-83ec2941d5d6",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:20:09.644700Z",
     "iopub.status.busy": "2024-02-14T09:20:09.644700Z",
     "iopub.status.idle": "2024-02-14T09:20:09.672504Z",
     "shell.execute_reply": "2024-02-14T09:20:09.669500Z",
     "shell.execute_reply.started": "2024-02-14T09:20:09.644700Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.6, 1.4, 0.2],\n",
       "       [5.4, 3.9, 1.7, 0.4],\n",
       "       [4.6, 3.4, 1.4, 0.3],\n",
       "       [5. , 3.4, 1.5, 0.2],\n",
       "       [4.4, 2.9, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.1],\n",
       "       [5.4, 3.7, 1.5, 0.2],\n",
       "       [4.8, 3.4, 1.6, 0.2],\n",
       "       [4.8, 3. , 1.4, 0.1],\n",
       "       [4.3, 3. , 1.1, 0.1],\n",
       "       [5.8, 4. , 1.2, 0.2],\n",
       "       [5.7, 4.4, 1.5, 0.4],\n",
       "       [5.4, 3.9, 1.3, 0.4],\n",
       "       [5.1, 3.5, 1.4, 0.3],\n",
       "       [5.7, 3.8, 1.7, 0.3],\n",
       "       [5.1, 3.8, 1.5, 0.3],\n",
       "       [5.4, 3.4, 1.7, 0.2],\n",
       "       [5.1, 3.7, 1.5, 0.4],\n",
       "       [4.6, 3.6, 1. , 0.2],\n",
       "       [5.1, 3.3, 1.7, 0.5],\n",
       "       [4.8, 3.4, 1.9, 0.2],\n",
       "       [5. , 3. , 1.6, 0.2],\n",
       "       [5. , 3.4, 1.6, 0.4],\n",
       "       [5.2, 3.5, 1.5, 0.2],\n",
       "       [5.2, 3.4, 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.6, 0.2],\n",
       "       [4.8, 3.1, 1.6, 0.2],\n",
       "       [5.4, 3.4, 1.5, 0.4],\n",
       "       [5.2, 4.1, 1.5, 0.1],\n",
       "       [5.5, 4.2, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.2, 1.2, 0.2],\n",
       "       [5.5, 3.5, 1.3, 0.2],\n",
       "       [4.9, 3.6, 1.4, 0.1],\n",
       "       [4.4, 3. , 1.3, 0.2],\n",
       "       [5.1, 3.4, 1.5, 0.2],\n",
       "       [5. , 3.5, 1.3, 0.3],\n",
       "       [4.5, 2.3, 1.3, 0.3],\n",
       "       [4.4, 3.2, 1.3, 0.2],\n",
       "       [5. , 3.5, 1.6, 0.6],\n",
       "       [5.1, 3.8, 1.9, 0.4],\n",
       "       [4.8, 3. , 1.4, 0.3],\n",
       "       [5.1, 3.8, 1.6, 0.2],\n",
       "       [4.6, 3.2, 1.4, 0.2],\n",
       "       [5.3, 3.7, 1.5, 0.2],\n",
       "       [5. , 3.3, 1.4, 0.2],\n",
       "       [7. , 3.2, 4.7, 1.4],\n",
       "       [6.4, 3.2, 4.5, 1.5],\n",
       "       [6.9, 3.1, 4.9, 1.5],\n",
       "       [5.5, 2.3, 4. , 1.3],\n",
       "       [6.5, 2.8, 4.6, 1.5],\n",
       "       [5.7, 2.8, 4.5, 1.3],\n",
       "       [6.3, 3.3, 4.7, 1.6],\n",
       "       [4.9, 2.4, 3.3, 1. ],\n",
       "       [6.6, 2.9, 4.6, 1.3],\n",
       "       [5.2, 2.7, 3.9, 1.4],\n",
       "       [5. , 2. , 3.5, 1. ],\n",
       "       [5.9, 3. , 4.2, 1.5],\n",
       "       [6. , 2.2, 4. , 1. ],\n",
       "       [6.1, 2.9, 4.7, 1.4],\n",
       "       [5.6, 2.9, 3.6, 1.3],\n",
       "       [6.7, 3.1, 4.4, 1.4],\n",
       "       [5.6, 3. , 4.5, 1.5],\n",
       "       [5.8, 2.7, 4.1, 1. ],\n",
       "       [6.2, 2.2, 4.5, 1.5],\n",
       "       [5.6, 2.5, 3.9, 1.1],\n",
       "       [5.9, 3.2, 4.8, 1.8],\n",
       "       [6.1, 2.8, 4. , 1.3],\n",
       "       [6.3, 2.5, 4.9, 1.5],\n",
       "       [6.1, 2.8, 4.7, 1.2],\n",
       "       [6.4, 2.9, 4.3, 1.3],\n",
       "       [6.6, 3. , 4.4, 1.4],\n",
       "       [6.8, 2.8, 4.8, 1.4],\n",
       "       [6.7, 3. , 5. , 1.7],\n",
       "       [6. , 2.9, 4.5, 1.5],\n",
       "       [5.7, 2.6, 3.5, 1. ],\n",
       "       [5.5, 2.4, 3.8, 1.1],\n",
       "       [5.5, 2.4, 3.7, 1. ],\n",
       "       [5.8, 2.7, 3.9, 1.2],\n",
       "       [6. , 2.7, 5.1, 1.6],\n",
       "       [5.4, 3. , 4.5, 1.5],\n",
       "       [6. , 3.4, 4.5, 1.6],\n",
       "       [6.7, 3.1, 4.7, 1.5],\n",
       "       [6.3, 2.3, 4.4, 1.3],\n",
       "       [5.6, 3. , 4.1, 1.3],\n",
       "       [5.5, 2.5, 4. , 1.3],\n",
       "       [5.5, 2.6, 4.4, 1.2],\n",
       "       [6.1, 3. , 4.6, 1.4],\n",
       "       [5.8, 2.6, 4. , 1.2],\n",
       "       [5. , 2.3, 3.3, 1. ],\n",
       "       [5.6, 2.7, 4.2, 1.3],\n",
       "       [5.7, 3. , 4.2, 1.2],\n",
       "       [5.7, 2.9, 4.2, 1.3],\n",
       "       [6.2, 2.9, 4.3, 1.3],\n",
       "       [5.1, 2.5, 3. , 1.1],\n",
       "       [5.7, 2.8, 4.1, 1.3],\n",
       "       [6.3, 3.3, 6. , 2.5],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [7.1, 3. , 5.9, 2.1],\n",
       "       [6.3, 2.9, 5.6, 1.8],\n",
       "       [6.5, 3. , 5.8, 2.2],\n",
       "       [7.6, 3. , 6.6, 2.1],\n",
       "       [4.9, 2.5, 4.5, 1.7],\n",
       "       [7.3, 2.9, 6.3, 1.8],\n",
       "       [6.7, 2.5, 5.8, 1.8],\n",
       "       [7.2, 3.6, 6.1, 2.5],\n",
       "       [6.5, 3.2, 5.1, 2. ],\n",
       "       [6.4, 2.7, 5.3, 1.9],\n",
       "       [6.8, 3. , 5.5, 2.1],\n",
       "       [5.7, 2.5, 5. , 2. ],\n",
       "       [5.8, 2.8, 5.1, 2.4],\n",
       "       [6.4, 3.2, 5.3, 2.3],\n",
       "       [6.5, 3. , 5.5, 1.8],\n",
       "       [7.7, 3.8, 6.7, 2.2],\n",
       "       [7.7, 2.6, 6.9, 2.3],\n",
       "       [6. , 2.2, 5. , 1.5],\n",
       "       [6.9, 3.2, 5.7, 2.3],\n",
       "       [5.6, 2.8, 4.9, 2. ],\n",
       "       [7.7, 2.8, 6.7, 2. ],\n",
       "       [6.3, 2.7, 4.9, 1.8],\n",
       "       [6.7, 3.3, 5.7, 2.1],\n",
       "       [7.2, 3.2, 6. , 1.8],\n",
       "       [6.2, 2.8, 4.8, 1.8],\n",
       "       [6.1, 3. , 4.9, 1.8],\n",
       "       [6.4, 2.8, 5.6, 2.1],\n",
       "       [7.2, 3. , 5.8, 1.6],\n",
       "       [7.4, 2.8, 6.1, 1.9],\n",
       "       [7.9, 3.8, 6.4, 2. ],\n",
       "       [6.4, 2.8, 5.6, 2.2],\n",
       "       [6.3, 2.8, 5.1, 1.5],\n",
       "       [6.1, 2.6, 5.6, 1.4],\n",
       "       [7.7, 3. , 6.1, 2.3],\n",
       "       [6.3, 3.4, 5.6, 2.4],\n",
       "       [6.4, 3.1, 5.5, 1.8],\n",
       "       [6. , 3. , 4.8, 1.8],\n",
       "       [6.9, 3.1, 5.4, 2.1],\n",
       "       [6.7, 3.1, 5.6, 2.4],\n",
       "       [6.9, 3.1, 5.1, 2.3],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [6.8, 3.2, 5.9, 2.3],\n",
       "       [6.7, 3.3, 5.7, 2.5],\n",
       "       [6.7, 3. , 5.2, 2.3],\n",
       "       [6.3, 2.5, 5. , 1.9],\n",
       "       [6.5, 3. , 5.2, 2. ],\n",
       "       [6.2, 3.4, 5.4, 2.3],\n",
       "       [5.9, 3. , 5.1, 1.8]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "275d6074-22fb-4a93-9945-f1fc84f911a9",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:20:45.653806Z",
     "iopub.status.busy": "2024-02-14T09:20:45.652795Z",
     "iopub.status.idle": "2024-02-14T09:20:45.663689Z",
     "shell.execute_reply": "2024-02-14T09:20:45.662623Z",
     "shell.execute_reply.started": "2024-02-14T09:20:45.653806Z"
    }
   },
   "outputs": [],
   "source": [
    "y_data = load_iris().target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "74050904-a7ec-41b6-a20b-72f9e509006f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:20:50.409573Z",
     "iopub.status.busy": "2024-02-14T09:20:50.409573Z",
     "iopub.status.idle": "2024-02-14T09:20:50.426602Z",
     "shell.execute_reply": "2024-02-14T09:20:50.421852Z",
     "shell.execute_reply.started": "2024-02-14T09:20:50.409573Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "906c2cb9-20af-4557-bf3a-a6a5abfd12a4",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:23:34.646747Z",
     "iopub.status.busy": "2024-02-14T09:23:34.646239Z",
     "iopub.status.idle": "2024-02-14T09:23:34.657343Z",
     "shell.execute_reply": "2024-02-14T09:23:34.656293Z",
     "shell.execute_reply.started": "2024-02-14T09:23:34.646239Z"
    }
   },
   "outputs": [],
   "source": [
    "x_data = pd.DataFrame(x_data,columns=['花萼长度','花萼宽度','花瓣长度','花瓣宽度'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "950576c9-1b31-4bb4-b771-44f86e4889b8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:24:16.746324Z",
     "iopub.status.busy": "2024-02-14T09:24:16.745645Z",
     "iopub.status.idle": "2024-02-14T09:24:16.764741Z",
     "shell.execute_reply": "2024-02-14T09:24:16.760662Z",
     "shell.execute_reply.started": "2024-02-14T09:24:16.746324Z"
    }
   },
   "outputs": [],
   "source": [
    "pd.set_option('display.unicode.east_asian_width',True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "265e4659-cab0-4b79-9a37-8cd1340969d7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:24:22.465419Z",
     "iopub.status.busy": "2024-02-14T09:24:22.462419Z",
     "iopub.status.idle": "2024-02-14T09:24:22.527538Z",
     "shell.execute_reply": "2024-02-14T09:24:22.525134Z",
     "shell.execute_reply.started": "2024-02-14T09:24:22.465419Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>花萼长度</th>\n",
       "      <th>花萼宽度</th>\n",
       "      <th>花瓣长度</th>\n",
       "      <th>花瓣宽度</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     花萼长度  花萼宽度  花瓣长度  花瓣宽度\n",
       "0         5.1       3.5       1.4       0.2\n",
       "1         4.9       3.0       1.4       0.2\n",
       "2         4.7       3.2       1.3       0.2\n",
       "3         4.6       3.1       1.5       0.2\n",
       "4         5.0       3.6       1.4       0.2\n",
       "..        ...       ...       ...       ...\n",
       "145       6.7       3.0       5.2       2.3\n",
       "146       6.3       2.5       5.0       1.9\n",
       "147       6.5       3.0       5.2       2.0\n",
       "148       6.2       3.4       5.4       2.3\n",
       "149       5.9       3.0       5.1       1.8\n",
       "\n",
       "[150 rows x 4 columns]"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "7d0c2d42-9839-4a65-9a30-b71fbad1fef2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:24:55.083534Z",
     "iopub.status.busy": "2024-02-14T09:24:55.082643Z",
     "iopub.status.idle": "2024-02-14T09:24:55.098013Z",
     "shell.execute_reply": "2024-02-14T09:24:55.096126Z",
     "shell.execute_reply.started": "2024-02-14T09:24:55.083534Z"
    }
   },
   "outputs": [],
   "source": [
    "x_data['类别'] = y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d06aea49-294d-460f-862d-241ba338787f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:24:59.990719Z",
     "iopub.status.busy": "2024-02-14T09:24:59.988715Z",
     "iopub.status.idle": "2024-02-14T09:25:00.013836Z",
     "shell.execute_reply": "2024-02-14T09:25:00.012758Z",
     "shell.execute_reply.started": "2024-02-14T09:24:59.990719Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>花萼长度</th>\n",
       "      <th>花萼宽度</th>\n",
       "      <th>花瓣长度</th>\n",
       "      <th>花瓣宽度</th>\n",
       "      <th>类别</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5.1</td>\n",
       "      <td>3.5</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.7</td>\n",
       "      <td>3.2</td>\n",
       "      <td>1.3</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.6</td>\n",
       "      <td>3.1</td>\n",
       "      <td>1.5</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5.0</td>\n",
       "      <td>3.6</td>\n",
       "      <td>1.4</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>6.7</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>6.3</td>\n",
       "      <td>2.5</td>\n",
       "      <td>5.0</td>\n",
       "      <td>1.9</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>6.5</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>6.2</td>\n",
       "      <td>3.4</td>\n",
       "      <td>5.4</td>\n",
       "      <td>2.3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>5.9</td>\n",
       "      <td>3.0</td>\n",
       "      <td>5.1</td>\n",
       "      <td>1.8</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     花萼长度  花萼宽度  花瓣长度  花瓣宽度  类别\n",
       "0         5.1       3.5       1.4       0.2     0\n",
       "1         4.9       3.0       1.4       0.2     0\n",
       "2         4.7       3.2       1.3       0.2     0\n",
       "3         4.6       3.1       1.5       0.2     0\n",
       "4         5.0       3.6       1.4       0.2     0\n",
       "..        ...       ...       ...       ...   ...\n",
       "145       6.7       3.0       5.2       2.3     2\n",
       "146       6.3       2.5       5.0       1.9     2\n",
       "147       6.5       3.0       5.2       2.0     2\n",
       "148       6.2       3.4       5.4       2.3     2\n",
       "149       5.9       3.0       5.1       1.8     2\n",
       "\n",
       "[150 rows x 5 columns]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "510a079c-be7f-4e4a-8edb-5123cb4a41a5",
   "metadata": {},
   "source": [
    "### 1.准备数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "94309ccd-1d8d-4a62-96b3-4a7c0f4f3baf",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:33:42.102079Z",
     "iopub.status.busy": "2024-02-14T09:33:42.100079Z",
     "iopub.status.idle": "2024-02-14T09:33:42.119754Z",
     "shell.execute_reply": "2024-02-14T09:33:42.116649Z",
     "shell.execute_reply.started": "2024-02-14T09:33:42.102079Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_iris\n",
    "import  numpy as np\n",
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a70aca7b-af71-41fc-899d-dd273124e654",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:33:42.530986Z",
     "iopub.status.busy": "2024-02-14T09:33:42.529986Z",
     "iopub.status.idle": "2024-02-14T09:33:42.551549Z",
     "shell.execute_reply": "2024-02-14T09:33:42.549565Z",
     "shell.execute_reply.started": "2024-02-14T09:33:42.530986Z"
    }
   },
   "outputs": [],
   "source": [
    "x_data = load_iris().data\n",
    "y_data = load_iris().target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "4ac8ef00-0164-4e2e-95f1-b9c4df3ad56a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:33:42.842122Z",
     "iopub.status.busy": "2024-02-14T09:33:42.842122Z",
     "iopub.status.idle": "2024-02-14T09:33:42.875572Z",
     "shell.execute_reply": "2024-02-14T09:33:42.874043Z",
     "shell.execute_reply.started": "2024-02-14T09:33:42.842122Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5.1, 3.5, 1.4, 0.2],\n",
       "       [4.9, 3. , 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.3, 0.2],\n",
       "       [4.6, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.6, 1.4, 0.2],\n",
       "       [5.4, 3.9, 1.7, 0.4],\n",
       "       [4.6, 3.4, 1.4, 0.3],\n",
       "       [5. , 3.4, 1.5, 0.2],\n",
       "       [4.4, 2.9, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.1],\n",
       "       [5.4, 3.7, 1.5, 0.2],\n",
       "       [4.8, 3.4, 1.6, 0.2],\n",
       "       [4.8, 3. , 1.4, 0.1],\n",
       "       [4.3, 3. , 1.1, 0.1],\n",
       "       [5.8, 4. , 1.2, 0.2],\n",
       "       [5.7, 4.4, 1.5, 0.4],\n",
       "       [5.4, 3.9, 1.3, 0.4],\n",
       "       [5.1, 3.5, 1.4, 0.3],\n",
       "       [5.7, 3.8, 1.7, 0.3],\n",
       "       [5.1, 3.8, 1.5, 0.3],\n",
       "       [5.4, 3.4, 1.7, 0.2],\n",
       "       [5.1, 3.7, 1.5, 0.4],\n",
       "       [4.6, 3.6, 1. , 0.2],\n",
       "       [5.1, 3.3, 1.7, 0.5],\n",
       "       [4.8, 3.4, 1.9, 0.2],\n",
       "       [5. , 3. , 1.6, 0.2],\n",
       "       [5. , 3.4, 1.6, 0.4],\n",
       "       [5.2, 3.5, 1.5, 0.2],\n",
       "       [5.2, 3.4, 1.4, 0.2],\n",
       "       [4.7, 3.2, 1.6, 0.2],\n",
       "       [4.8, 3.1, 1.6, 0.2],\n",
       "       [5.4, 3.4, 1.5, 0.4],\n",
       "       [5.2, 4.1, 1.5, 0.1],\n",
       "       [5.5, 4.2, 1.4, 0.2],\n",
       "       [4.9, 3.1, 1.5, 0.2],\n",
       "       [5. , 3.2, 1.2, 0.2],\n",
       "       [5.5, 3.5, 1.3, 0.2],\n",
       "       [4.9, 3.6, 1.4, 0.1],\n",
       "       [4.4, 3. , 1.3, 0.2],\n",
       "       [5.1, 3.4, 1.5, 0.2],\n",
       "       [5. , 3.5, 1.3, 0.3],\n",
       "       [4.5, 2.3, 1.3, 0.3],\n",
       "       [4.4, 3.2, 1.3, 0.2],\n",
       "       [5. , 3.5, 1.6, 0.6],\n",
       "       [5.1, 3.8, 1.9, 0.4],\n",
       "       [4.8, 3. , 1.4, 0.3],\n",
       "       [5.1, 3.8, 1.6, 0.2],\n",
       "       [4.6, 3.2, 1.4, 0.2],\n",
       "       [5.3, 3.7, 1.5, 0.2],\n",
       "       [5. , 3.3, 1.4, 0.2],\n",
       "       [7. , 3.2, 4.7, 1.4],\n",
       "       [6.4, 3.2, 4.5, 1.5],\n",
       "       [6.9, 3.1, 4.9, 1.5],\n",
       "       [5.5, 2.3, 4. , 1.3],\n",
       "       [6.5, 2.8, 4.6, 1.5],\n",
       "       [5.7, 2.8, 4.5, 1.3],\n",
       "       [6.3, 3.3, 4.7, 1.6],\n",
       "       [4.9, 2.4, 3.3, 1. ],\n",
       "       [6.6, 2.9, 4.6, 1.3],\n",
       "       [5.2, 2.7, 3.9, 1.4],\n",
       "       [5. , 2. , 3.5, 1. ],\n",
       "       [5.9, 3. , 4.2, 1.5],\n",
       "       [6. , 2.2, 4. , 1. ],\n",
       "       [6.1, 2.9, 4.7, 1.4],\n",
       "       [5.6, 2.9, 3.6, 1.3],\n",
       "       [6.7, 3.1, 4.4, 1.4],\n",
       "       [5.6, 3. , 4.5, 1.5],\n",
       "       [5.8, 2.7, 4.1, 1. ],\n",
       "       [6.2, 2.2, 4.5, 1.5],\n",
       "       [5.6, 2.5, 3.9, 1.1],\n",
       "       [5.9, 3.2, 4.8, 1.8],\n",
       "       [6.1, 2.8, 4. , 1.3],\n",
       "       [6.3, 2.5, 4.9, 1.5],\n",
       "       [6.1, 2.8, 4.7, 1.2],\n",
       "       [6.4, 2.9, 4.3, 1.3],\n",
       "       [6.6, 3. , 4.4, 1.4],\n",
       "       [6.8, 2.8, 4.8, 1.4],\n",
       "       [6.7, 3. , 5. , 1.7],\n",
       "       [6. , 2.9, 4.5, 1.5],\n",
       "       [5.7, 2.6, 3.5, 1. ],\n",
       "       [5.5, 2.4, 3.8, 1.1],\n",
       "       [5.5, 2.4, 3.7, 1. ],\n",
       "       [5.8, 2.7, 3.9, 1.2],\n",
       "       [6. , 2.7, 5.1, 1.6],\n",
       "       [5.4, 3. , 4.5, 1.5],\n",
       "       [6. , 3.4, 4.5, 1.6],\n",
       "       [6.7, 3.1, 4.7, 1.5],\n",
       "       [6.3, 2.3, 4.4, 1.3],\n",
       "       [5.6, 3. , 4.1, 1.3],\n",
       "       [5.5, 2.5, 4. , 1.3],\n",
       "       [5.5, 2.6, 4.4, 1.2],\n",
       "       [6.1, 3. , 4.6, 1.4],\n",
       "       [5.8, 2.6, 4. , 1.2],\n",
       "       [5. , 2.3, 3.3, 1. ],\n",
       "       [5.6, 2.7, 4.2, 1.3],\n",
       "       [5.7, 3. , 4.2, 1.2],\n",
       "       [5.7, 2.9, 4.2, 1.3],\n",
       "       [6.2, 2.9, 4.3, 1.3],\n",
       "       [5.1, 2.5, 3. , 1.1],\n",
       "       [5.7, 2.8, 4.1, 1.3],\n",
       "       [6.3, 3.3, 6. , 2.5],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [7.1, 3. , 5.9, 2.1],\n",
       "       [6.3, 2.9, 5.6, 1.8],\n",
       "       [6.5, 3. , 5.8, 2.2],\n",
       "       [7.6, 3. , 6.6, 2.1],\n",
       "       [4.9, 2.5, 4.5, 1.7],\n",
       "       [7.3, 2.9, 6.3, 1.8],\n",
       "       [6.7, 2.5, 5.8, 1.8],\n",
       "       [7.2, 3.6, 6.1, 2.5],\n",
       "       [6.5, 3.2, 5.1, 2. ],\n",
       "       [6.4, 2.7, 5.3, 1.9],\n",
       "       [6.8, 3. , 5.5, 2.1],\n",
       "       [5.7, 2.5, 5. , 2. ],\n",
       "       [5.8, 2.8, 5.1, 2.4],\n",
       "       [6.4, 3.2, 5.3, 2.3],\n",
       "       [6.5, 3. , 5.5, 1.8],\n",
       "       [7.7, 3.8, 6.7, 2.2],\n",
       "       [7.7, 2.6, 6.9, 2.3],\n",
       "       [6. , 2.2, 5. , 1.5],\n",
       "       [6.9, 3.2, 5.7, 2.3],\n",
       "       [5.6, 2.8, 4.9, 2. ],\n",
       "       [7.7, 2.8, 6.7, 2. ],\n",
       "       [6.3, 2.7, 4.9, 1.8],\n",
       "       [6.7, 3.3, 5.7, 2.1],\n",
       "       [7.2, 3.2, 6. , 1.8],\n",
       "       [6.2, 2.8, 4.8, 1.8],\n",
       "       [6.1, 3. , 4.9, 1.8],\n",
       "       [6.4, 2.8, 5.6, 2.1],\n",
       "       [7.2, 3. , 5.8, 1.6],\n",
       "       [7.4, 2.8, 6.1, 1.9],\n",
       "       [7.9, 3.8, 6.4, 2. ],\n",
       "       [6.4, 2.8, 5.6, 2.2],\n",
       "       [6.3, 2.8, 5.1, 1.5],\n",
       "       [6.1, 2.6, 5.6, 1.4],\n",
       "       [7.7, 3. , 6.1, 2.3],\n",
       "       [6.3, 3.4, 5.6, 2.4],\n",
       "       [6.4, 3.1, 5.5, 1.8],\n",
       "       [6. , 3. , 4.8, 1.8],\n",
       "       [6.9, 3.1, 5.4, 2.1],\n",
       "       [6.7, 3.1, 5.6, 2.4],\n",
       "       [6.9, 3.1, 5.1, 2.3],\n",
       "       [5.8, 2.7, 5.1, 1.9],\n",
       "       [6.8, 3.2, 5.9, 2.3],\n",
       "       [6.7, 3.3, 5.7, 2.5],\n",
       "       [6.7, 3. , 5.2, 2.3],\n",
       "       [6.3, 2.5, 5. , 1.9],\n",
       "       [6.5, 3. , 5.2, 2. ],\n",
       "       [6.2, 3.4, 5.4, 2.3],\n",
       "       [5.9, 3. , 5.1, 1.8]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "afd20350-36d4-45a9-8fba-59c28558da8d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:33:43.000596Z",
     "iopub.status.busy": "2024-02-14T09:33:43.000596Z",
     "iopub.status.idle": "2024-02-14T09:33:43.015170Z",
     "shell.execute_reply": "2024-02-14T09:33:43.014143Z",
     "shell.execute_reply.started": "2024-02-14T09:33:43.000596Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
       "       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,\n",
       "       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,\n",
       "       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "fd1319cb-d0b5-4937-b11b-3373e364f0d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:33:43.786304Z",
     "iopub.status.busy": "2024-02-14T09:33:43.786304Z",
     "iopub.status.idle": "2024-02-14T09:33:43.793983Z",
     "shell.execute_reply": "2024-02-14T09:33:43.792300Z",
     "shell.execute_reply.started": "2024-02-14T09:33:43.786304Z"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "数据集乱序\n",
    "\"\"\"\n",
    "np.random.seed(116)\n",
    "np.random.shuffle(x_data)\n",
    "np.random.seed(116)\n",
    "np.random.shuffle(y_data)\n",
    "tf.random.set_seed(116)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "208d1a9c-f4e4-4c18-a542-69bd30af9724",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:36:25.234611Z",
     "iopub.status.busy": "2024-02-14T09:36:25.234611Z",
     "iopub.status.idle": "2024-02-14T09:36:25.244094Z",
     "shell.execute_reply": "2024-02-14T09:36:25.242578Z",
     "shell.execute_reply.started": "2024-02-14T09:36:25.234611Z"
    }
   },
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "数据集拆分\n",
    "\"\"\"\n",
    "x_train = x_data[:-30]\n",
    "y_train = y_data[:-30]\n",
    "x_test = x_data[-30:]\n",
    "y_test = y_data[-30:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "bf03bd33-28ac-4676-9cd4-1caf945513af",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:38:46.731567Z",
     "iopub.status.busy": "2024-02-14T09:38:46.731567Z",
     "iopub.status.idle": "2024-02-14T09:38:49.823032Z",
     "shell.execute_reply": "2024-02-14T09:38:49.821030Z",
     "shell.execute_reply.started": "2024-02-14T09:38:46.731567Z"
    }
   },
   "outputs": [],
   "source": [
    "train_db = tf.data.Dataset.from_tensor_slices((x_train,y_train)).batch(32)\n",
    "test_db = tf.data.Dataset.from_tensor_slices((x_test,y_test)).batch(32)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52233ef8-6463-420e-b54e-3011bb67b9c5",
   "metadata": {},
   "source": [
    "### 2. 搭建网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "da3460c2-a00d-4818-82d6-b108ef299a46",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T09:42:09.343915Z",
     "iopub.status.busy": "2024-02-14T09:42:09.342915Z",
     "iopub.status.idle": "2024-02-14T09:42:09.378876Z",
     "shell.execute_reply": "2024-02-14T09:42:09.376658Z",
     "shell.execute_reply.started": "2024-02-14T09:42:09.343915Z"
    }
   },
   "outputs": [],
   "source": [
    "w1 = tf.Variable(tf.random.truncated_normal([4,3],stddev=0.1,seed=1))\n",
    "b1 = tf.Variable(tf.random.truncated_normal([3],stddev=0.1,seed=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "864426ed-f4ce-4f08-af25-ccb428903406",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:18:18.332075Z",
     "iopub.status.busy": "2024-02-14T10:18:18.331011Z",
     "iopub.status.idle": "2024-02-14T10:18:18.357164Z",
     "shell.execute_reply": "2024-02-14T10:18:18.353611Z",
     "shell.execute_reply.started": "2024-02-14T10:18:18.332075Z"
    }
   },
   "outputs": [],
   "source": [
    "def train_iris(train_db,epoch=5):\n",
    "   lr = 0.2\n",
    "   for epoch in range(epoch):\n",
    "    for step,(x_train,y_train) in enumerate(train_db):\n",
    "        with tf.GradientTape() as tape:\n",
    "             w1 = tf.Variable(tf.constant(0.0))\n",
    "             b1 = tf.Variable(tf.constant(0.0))\n",
    "             loss = tf.pow(w1+1,2)\n",
    "        grads = tape.gradient(loss,w1)\n",
    "        w1.assign_sub(lr*grads)\n",
    "    print(\"Epoch{},loss:{}\".format(epoch,loss))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "4d78807b-471c-4f4d-932d-aa44cc6973d8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-14T10:18:19.093780Z",
     "iopub.status.busy": "2024-02-14T10:18:19.092279Z",
     "iopub.status.idle": "2024-02-14T10:18:19.262116Z",
     "shell.execute_reply": "2024-02-14T10:18:19.259099Z",
     "shell.execute_reply.started": "2024-02-14T10:18:19.093780Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch0,loss:1.0\n",
      "Epoch1,loss:1.0\n",
      "Epoch2,loss:1.0\n",
      "Epoch3,loss:1.0\n",
      "Epoch4,loss:1.0\n"
     ]
    }
   ],
   "source": [
    "train_iris(train_db)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf4c1f18-8411-40b3-a895-db98f9896e6a",
   "metadata": {},
   "source": [
    "### 3. 参数优化"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2255efc5-ad8f-40c9-adf2-a03146810f90",
   "metadata": {},
   "source": [
    "### 4. 测试结果"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1afd59e-86ee-4dcd-a01d-64e9f3b63282",
   "metadata": {},
   "source": [
    "### 5. acc/loss 可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe2114d9-f91a-42a4-926e-051e676c7906",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf2.10",
   "language": "python",
   "name": "tf2.10"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  },
  "scenes_data": {
   "active_scene": "Default Scene",
   "init_scene": "",
   "scenes": [
    "Default Scene"
   ]
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
